package com.coocaa.x.service.litedaemon.daemons.tvpie.app.util;

import android.text.TextUtils;

import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.litedaemon.daemons.tvpie.app.TVPieAppDaemon;
import com.coocaa.x.service.litedaemon.daemons.tvpie.app.bean.AppResultBean;
import com.coocaa.x.service.litepm.data.AppInfo;
import com.coocaa.x.service.litepm.data.MActivityInfo;
import com.skyworth.framework.skysdk.properties.SkyAppsProperties;
import com.skyworth.framework.skysdk.properties.SkyGeneralProperties;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstalledFilter {

    public static ArrayList<AppResultBean.GetInstalled> getInstalledApp(List<AppInfo> infos) {
        ArrayList<AppResultBean.GetInstalled> result = new ArrayList<AppResultBean.GetInstalled>();
        if (infos == null || infos.isEmpty()) {
            return result;
        }

        Map<String, Boolean> mAppMap = getConfigString();
        int usedTime = 1;
        for (AppInfo info : infos) {
            LogUtils.i(TVPieAppDaemon.TAG, "appName:" + info.appName + " pkgName=" + info
                    .pkgName + " version:" + info.versionCode);
            if (!info.isSystemApp) {
                if (TextUtils.isEmpty(info.appName) || TextUtils.isEmpty(info.pkgName)) {
                    continue;
                }
                // Todo, usedtime
//				try {
//					usedTime = ServiceFacotry.getAppService().getAppUserdTimes(info.mainActivity);
//				} catch (RemoteException e) {
//					e.printStackTrace();
//				} catch (ServiceFacotry.ServiceNotConnectException e) {
//					e.printStackTrace();
//				}
                AppResultBean.GetInstalled gi = new AppResultBean.GetInstalled(info);
                gi.usedTimes = usedTime;
                result.add(gi);
            } else {

                if (TextUtils.isEmpty(info.appName) || TextUtils.isEmpty(info.pkgName)) {
                    continue;
                }
                // 酷开系统自带应用，微信相册之类。(读配置文件获取该参数，若没有该配置文件，全部显示)
                Boolean isMyApp = mAppMap.get(info.pkgName);
                LogUtils.i(TVPieAppDaemon.TAG, info.appName + " is systemApp, isMyApp:" +
                        ((isMyApp == null || isMyApp) ? "true" : "false"));
                if (info.pkgName.equals("com.tianci.appstore")) {
                    if (info.activityList != null && info.activityList.size() > 1) {
                        List<MActivityInfo> mList = info.activityList;
                        for (MActivityInfo aInfo : mList) {
                            if (aInfo.className.equals("com.coocaa.app.gamecenter.pages.home.GameMainActivity")
                                    && !canShowGame()) {
                                continue;
                            }
                            if (aInfo.className.equals("com.coocaa.launcher.EasyLauncherActivity")
                                    && !canShowLaunch()) {
                                continue;
                            }
//							 try {
//								 usedTime = ServiceFacotry.getAppService().getAppUserdTimes
//										 (aInfo.className);
//							 } catch (RemoteException e) {
//								 e.printStackTrace();
//							 } catch (ServiceFacotry.ServiceNotConnectException e) {
//								 e.printStackTrace();
//							 }
                            AppResultBean.GetInstalled gi = new AppResultBean.GetInstalled(info);
                            gi.usedTimes = usedTime;
                            gi.appName = aInfo.name;
                            gi.mainActivity = aInfo.className;
                            result.add(0, gi);
                        }
                    }
                } else if (isMyApp == null || isMyApp) {
//					 try {
//						 usedTime = ServiceFacotry.getAppService().getAppUserdTimes(info.mainActivity);
//					 } catch (RemoteException e) {
//						 e.printStackTrace();
//					 } catch (ServiceFacotry.ServiceNotConnectException e) {
//						 e.printStackTrace();
//					 }
                    AppResultBean.GetInstalled gi = new AppResultBean.GetInstalled(info);
                    gi.usedTimes = usedTime;
                    result.add(gi);
                }
            }
        }
        return result;
    }

    public static boolean canShowGame() {
        boolean isGameShow = true;
        String launcher_entry = SkyGeneralProperties.getProperty("x_gamecenter_entry");
        if (launcher_entry != null) {
            isGameShow = Boolean.parseBoolean(launcher_entry);
        } else {
            isGameShow = true;
        }
        return isGameShow;
    }

    public static boolean canShowLaunch() {
        boolean isLauncherShow = true;
        String launcher_entry = SkyGeneralProperties.getProperty("x_launcher_entry");
        if (launcher_entry != null) {
            isLauncherShow = Boolean.parseBoolean(launcher_entry);
        } else {
            isLauncherShow = true;
        }
        return isLauncherShow;
    }

    public static Map<String, Boolean> getConfigString() {
        Map<String, Boolean> mAppMap = new HashMap<String, Boolean>();
        List<String> appDatas = new ArrayList<String>();
        appDatas = SkyAppsProperties.getPropertyList();
        for (final String pkg : appDatas) {
            String prop = SkyAppsProperties.getProperty(pkg);
            if (prop.contains("myapp")) {
                mAppMap.put(pkg, true);
            } else {
                mAppMap.put(pkg, false);
            }
        }
        return mAppMap;
    }


}
